package com.xnx3.kefu.inset.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.Lang;
import com.xnx3.j2ee.service.SqlCacheService;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.kefu.core.entity.Kefu;
import com.xnx3.kefu.core.entity.Zuoxi;
import com.xnx3.kefu.core.service.KefuService;
import com.xnx3.kefu.core.vo.KefuVO;
import com.xnx3.wangmarket.plugin.api.controller.BaseController;
import com.xnx3.wangmarket.plugin.api.entity.InsetKefu;
import com.xnx3.wangmarket.plugin.api.vo.CreateKefuVO;

/**
 * 客服对接
 * 已废弃。使用 wangmarket.plugin.api.KefuController
 * @author 管雷鸣
 */
@Deprecated
@Controller(value="InsetKefuController")
@RequestMapping("/kefu/inset/admin/")
public class KefuController extends BaseController {
	@Resource
	private SqlService sqlService;
	@Resource
	private UserService userService;
	@Resource
	private SqlCacheService sqlCacheService;
	@Resource
	private KefuService kefuService;

	/**
	 * 创建客服平台，返回自动登录客服平台的token。 如果传入的这个otherid已经创建过一次了，那么返回的是第一次执行时返回的token。 
	 * @param otherid 第三方平台的用户id
	 * @param identity 身份验证， application.properties 中配置的 kefu.inset.identity 的值。（如果kefu.inset.identity.wangmarket设为true，那么你就要传你的授权码了，当然这种情况基本用不到。）
	 * @return 这个客服平台的登录token
	 */
	@RequestMapping(value="createKefu${api.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public CreateKefuVO createKefu(HttpServletRequest request,Model model,
			@RequestParam(value = "otherid", required = false, defaultValue = "") String otherid,
			@RequestParam(value = "identity", required = false, defaultValue = "") String identity){
		CreateKefuVO vo = new CreateKefuVO();
		
		//验证 identity
		com.xnx3.BaseVO checkIdentityVO = checkIdentity(identity);
		if(checkIdentityVO.getResult() - BaseVO.FAILURE == 0){
			vo.setBaseVO(BaseVO.FAILURE, checkIdentityVO.getInfo());
			return vo;
		}
		
		
		if(otherid.length() < 1){
			vo.setBaseVO(BaseVO.FAILURE, "请传入 otherid");
			return vo;
		}
		
		//判断一下是否已经有了这个otherid关联了
		InsetKefu inset = sqlCacheService.findAloneByProperty(InsetKefu.class, "otherid", otherid);
		if(inset == null){
			//创建新记录
			
			//创建客服平台
			KefuVO kefuvo = kefuService.createKefu(request);
			if(kefuvo.getResult() - KefuVO.FAILURE == 0){
				vo.setBaseVO(BaseVO.FAILURE, kefuvo.getInfo());
				return vo;
			}
			
			inset = new InsetKefu();
			inset.setOtherid(otherid);
			inset.setPassword(Lang.uuid()+Lang.uuid());
			inset.setKefuid(kefuvo.getKefu().getId());
			sqlService.save(inset);
			
			vo.setZuoxiid(kefuvo.getZuoxiid());
		}else{
			Zuoxi zuoxi = sqlCacheService.findAloneByProperty(Zuoxi.class, "kefuid", inset.getKefuid());
			vo.setZuoxiid(zuoxi.getId());
		}
		
		vo.setPassword(inset.getPassword());
		return vo;
	}
	
	
}
